//
//  IGArticleVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/28.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGArticleVC.h"
#import "IGArticleCell.h"
#import "IGMainSearchApi.h"
#import "IGNetReachable.h"
#import "ZTXQViewController.h"
#import "IGEmptyView.h"

@interface IGArticleVC ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, strong) NSMutableArray *specialMuArr;
@property (nonatomic, strong) IGMainSearchApi *searchApi;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation IGArticleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    self.page = 0;
    self.searchApi = [IGMainSearchApi new];
    self.searchApi.type = IGSearchTypeSpecial;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf requestData];
    }];
}

- (NSMutableArray *)specialMuArr {
    if (!_specialMuArr) {
        _specialMuArr = [NSMutableArray array];
    }
    return _specialMuArr;
}

- (void)setupFooter {
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        weakSelf.page++;
        
        [weakSelf requestData];
    }];
}

- (void)setSearchKey:(NSString *)searchKey {
    if (_searchKey == searchKey || !searchKey || [searchKey isEqualToString:@""]) {
        return;
    }
    _searchKey = searchKey;
    self.page = 0;
    [self requestData];
}

- (void)requestData {
    self.searchApi.page = self.page;
    self.searchApi.searchKey = self.searchKey;
    __weak typeof(self) weakSelf = self;
    [self.searchApi startWithDecodeClass:[IGMainSearchResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.tableView ig_endHeaderRefreshing];
        [weakSelf.tableView ig_endFooterRefreshing];
        if (responseObject) {
            IGMainSearchResp *resp = responseObject;
            IGMainSearchModel *model = resp.data;
            
            NSArray *tmpArr = model.lstSpecial;
            if (tmpArr && tmpArr.count > 0) {
                [weakSelf setupFooter];
            } else {
                [weakSelf.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 0) {
                weakSelf.specialMuArr = [NSMutableArray arrayWithArray:tmpArr];
            } else {
                [weakSelf.specialMuArr addObjectsFromArray:tmpArr];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.specialMuArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IGArticleCell *cell = [IGArticleCell cellWithTableView:tableView];
    IGSpecial *special = self.specialMuArr[indexPath.row];
    cell.special = special;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IGSpecial *special = [self.specialMuArr objectAtIndex:indexPath.row];
    ZTXQViewController *vc = [[ZTXQViewController alloc] init];
    vc.s_id = special.sid;
    [UIViewController ig_pushViewController:vc animated:YES];
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
//    emptyView.title = @"暂无数据~";
    emptyView.title = NSLocalizedString(@"No Data", nil);
    emptyView.requestApi = self.searchApi;
    return emptyView;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
